import * as userAPI from "../../api/userAPI";
import Message from "element-ui/packages/message/src/main";

const userStorage = {
    state: {},
    mutations: {},
    actions: {
        register: async ({commit}, data) => {
            try {
                let res = await userAPI.registerAPI(data);
                if (!res) {
                    Message({
                        message: "注册失败",
                        type: "error"
                    });
                }
                return res;
            } catch (e) {
                return {code: 500};
            }
        },
        findPassword: async ({commit}, data) => {
            try {
                let res = await userAPI.findPasswordAPI(data);
                if (!res) {
                    Message({
                        message: "找回密码失败",
                        type: "error"
                    });
                }
                return res;
            } catch (e) {
                return {code: 500};
            }
        },
        getUserInfo: async ({commit}, data) => {
            try {
                let res = await userAPI.getUserInfoAPI(data);
                if (!res) {
                    Message({
                        message: "获取用户信息失败",
                        type: "error"
                    });
                }
                return res;
            } catch (e) {
                return {code: 500};
            }
        },
        sendMailCode: async ({commit}, data) => {
            try {
                let res = await userAPI.sendMailCodeAPI(data);
                if (!res) {
                    Message({
                        message: "发送邮箱验证码失败",
                        type: "error"
                    });
                }
                return res;
            } catch (e) {
                return {code: 500};
            }
        },
        sendMailCodeL: async ({commit}, data) => {
            try {
                let res = await userAPI.sendMailCodeLAPI(data);
                if (!res) {
                    Message({
                        message: "发送邮箱验证码失败",
                        type: "error"
                    });
                }
                return res;
            } catch (e) {
                return {code: 500};
            }
        },
        updatePassword: async ({commit}, data) => {
            try {
                let res = await userAPI.updatePasswordAPI(data);
                if (!res) {
                    Message({
                        message: "修改密码失败",
                        type: "error"
                    });
                }
                return res;
            } catch (e) {
                return {code: 500};
            }
        },
    }

};

export default userStorage;